﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;

namespace FisheryPlatform.Search.Core.Analysis
{
    /// <summary>
    /// MMKeywordFilter: Segment CN-Tokens using MMWordIdentifier to find keyword-Token.
    /// </summary>
    /// <author>gwd, 2006-5-26</author>
    public class MMKeywordFilter : MMSegmentFilter
    {

        public MMKeywordFilter(MMAnalyzer analyzer, TokenStream input, string dictionaryName)
            : base(analyzer, input, dictionaryName)
        { }

        /// <summary>
        /// 1. Contact continuous single CN Character to CN-Token.
        /// 2. Add Long_than_one word as CN_WORD Token.
        /// </summary>
        /// <param name="startOffset"></param>
        /// <param name="words"></param>
        protected override void AddToken(int startOffset, string[] words)
        {
            int wordLength;
            string content = "";
            for (int j = 0; j < words.Length; j++)
            {
                wordLength = words[j].Length;
                if (wordLength > 1)
                {
                    if (content.Length > 0)
                    {
                        _tokens.Add(new Token(content, startOffset, startOffset + content.Length - 1, "CN"));
                        startOffset += content.Length;
                        content = "";
                    }

                    _tokens.Add(new Token(words[j], startOffset, startOffset + wordLength - 1, "CN_WORD"));
                    startOffset += wordLength;
                }
                else
                {
                    content += words[j];
                }
            }

            // deal with the left words
            if (content.Length > 0)
            {
                _tokens.Add(new Token(content, startOffset, startOffset + content.Length - 1, "CN"));
            }
        }


    }
}
